import { WeaTableNew, WeaForm } from 'comsMobx';
import { WeaLocaleProvider } from 'ecCom';
import { observable, action, runInAction } from 'mobx';
import { Base } from '../base';
import objectAssign from 'object-assign';
import * as API from '../../apis/signlog/UpdateSignApi';

const { getLabel } = WeaLocaleProvider;
const { TableStore } = WeaTableNew;

export default class UpdateSignLogStore extends Base {
  @observable loading = false;
  @observable signinput = {};
  @observable signListStore = new TableStore();
  @observable showSignInput = false;

  sumitParams = {}
  coreParams = {}
  refid = 'update';
  logid = '';
  savenumber = 0;
  form = new WeaForm();// 高级搜索
  @observable conditions = [];


  @action
  changeSignInput = (params = {}) => {
    let temsignparams = this.signinput || {};
    if (params.signdocids != undefined) {
      const { signdocids } = params;
      let signdocreplacedata = [];
      signdocids.split(',').map((id) => {
        if (id) signdocreplacedata.push({ id });
      });
      params.signdocreplacedata = signdocreplacedata;

      params.remarkInfo = { ...params };
    }
    if (params.signworkflowids != undefined) {
      const { signworkflowids } = params;
      let signworkflowreplacedata = [];
      signworkflowids.split(',').map((id) => {
        if (id) signworkflowreplacedata.push({ id });
      });
      params.signworkflowreplacedata = signworkflowreplacedata;
      params.remarkInfo = { ...params };
    }
    if (params.handWrittenSign) {
      params.remarkInfo = { ...params };
    }
    if (temsignparams && temsignparams.remarkInfo && params.remarkInfo) {
      temsignparams.remarkInfo = { ...temsignparams.remarkInfo, ...params.remarkInfo };
    }
    this.signinput = { ...params, ...temsignparams };
  }

  changeSubmitParams = (datas) => {
    this.sumitParams = objectAssign({}, this.sumitParams, datas);
  }

  initCoreParams = (params) => {
    this.coreParams = params;
    this.init();
    this.loadForm();
  }

  loadForm = () => {
    // 加载高级搜索相关
    API.getSignLogForm(this.coreParams).then((res) => {
      const { conditions, api_status, api_message } = res;
      if (api_status) {
        runInAction(() => {
          this.form.initFormFields(conditions);
          this.conditions = conditions;
        });
      } else {
        this.showErrorMsg(api_message || getLabel(388768, '加载失败!'), 3);
      }
    });
  }


  @action.bound
  init = () => {
    this.loading = true;
    const { form, coreParams } = this;
    let params = objectAssign({}, (form && form.getFormParams()) || {}, coreParams);
    API.getSignLogSessionkey(params).then((res) => {
      const { sessionkey } = res;
      this.signListStore.getDatasCallback = () => {
        this.loading = false;
      };
      this.signListStore.getDatas(sessionkey, 1);
    });
  }

  doSave = (workflowStores = {}, onCancel) => {
    const { globalStore = {} } = workflowStores;
    const { submitParam = {} } = globalStore;
    let params = objectAssign({ logid: this.logid }, this.coreParams, submitParam, this.sumitParams);
    this.loading = false;

    if (window.CK_REF && window.CK_REF[`${this.refid}Edit`]) {
      // params.remark = window.CK_REF[this.refid + "Edit"].getEditor().getData() || remark;
      params.remark = window.CK_REF[`${this.refid}Edit`].getEditor().getData();
    }
    API.doUpdateSign(params).then((res) => {
      const { api_status = false, api_message = '' } = res;
      this.loading = true;
      if (api_status) {
        this.init();
        this.savenumber++;
        this.showSuccessMsg(getLabel(83551, '保存成功！'), 3, () => onCancel(res));
      } else {
        this.showErrorMsg(api_message);
      }
    });
  }

  @action.bound
  getSignInput = (para = { logid: '' }) => {
    this.loading = false;
    let params = objectAssign({}, this.coreParams, this.sumitParams, para);
    this.showSignInput = true;
    this.logid = para.logid;

    API.getSignInput(params).then((datas) => {
      runInAction(() => {
        this.loading = true;
        this.signinput = datas;
      });
    });
  }

  doDeleteSignLogByLogId = (data = { logid: '' }) => {
    let params = objectAssign({}, this.coreParams, this.sumitParams, data);
    // 删除成功刷新数据，删除失败提示失败
    API.doDeleteSign(params).then((res) => {
      const { api_status = false, api_message } = res;
      // this.reload();
      if (api_status) { // 删除成功准备刷新数据
        this.init();
        this.savenumber++;
        this.showSuccessMsg(api_message || getLabel(83472, '删除成功！'));
      } else { // 删除失败提示信息
        this.showErrorMsg(api_message || getLabel(83473, '删除失败！'));
      }
    });
  }

  closeShowSignInput = () => {
    this.showSignInput = false;
    this.sumitParams = {};
    this.loading = false;
    this.signinput = {};
  }

	/**
	 * 注销数据
	 */
  destory = () => {
    this.signinput = {};
  }
}
